import ENV_URL from '@/utils/url';
class RequestClass {
    constructor(_baseUrl, _header = {}) {
        this.baseUrl = _baseUrl || ENV_URL.baseUrl
        this.header = Object.assign({
            token: uni.getStorageSync("token"),
            timeout: 6000 * 10 // 超时时间，单位 ms 默认: 10s
        }, _header)
    }

    setHeader (_header) {
        this.header = Object.assign(this.header, _header)
    }

    setBaseUrl (_baseUrl) {
        this.baseUrl = _baseUrl
    }

    request (_data) {
        console.log(this.header);
        // 处理数据
        uni.showLoading({ mask: true, title: "加载中..." })
        return new Promise((resolve, reject) => {
            uni.request(Object.assign(_data, {
                url: this.baseUrl + _data.url,
                header: this.header,
                success: (res) => {
                    if (res.statusCode === 200)
                    resolve(res)
                    else
                    reject(err)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    uni.hideLoading()
                }
            }))
        })
    }
}

export default RequestClass